package com.aaa.sbm.service;

import com.aaa.sbm.util.FtpProperties;
import com.aaa.sbm.util.HandlerFtpFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @fileName:HandlerFileServiceImpl
 * @description:通用上传工具类
 * @author:zz
 * @createTime:2020/10/31 11:44
 * @version:1.0.0
 */
@Service
public class HandlerFileServiceImpl implements HandlerFileService {


    @Autowired
    private FtpProperties ftpProperties;

    @Override
    public Map fileUpload(MultipartFile multipartFile, String savePath) {
     //String host, int port, String userName, String passWord
      //, String basePath, String savePath, String fileName, InputStream inputStream
        //获取源文件名称
        String originalFilename = multipartFile.getOriginalFilename();
        //获取源文件后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //组装新文件名称
        String fileName = UUID.randomUUID()+suffix;
        try {
            //调用工具类上传
            HandlerFtpFileUtil.uploadFile(ftpProperties.getHost(),ftpProperties.getPort()
            ,ftpProperties.getUsername(),ftpProperties.getPassword(),ftpProperties.getBasePath(),
                    savePath,fileName,multipartFile.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 实例化返回对象
        Map map  = new HashMap();
//        filePath=ftpProperties.getHost()+"/"+savePath+"/"+fileName;
        map.put("filePath","http://"+ftpProperties.getHost()+"/"+savePath+"/"+fileName);
        map.put("oldFileName",originalFilename);
        return map;
    }
}
